package com.shengde.jinwawaforleader.model.myJob.everyRecipe;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.model.myJob.adapter.FoodExpandableListAdapter;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProEveryRecipe;

import java.util.ArrayList;
import java.util.List;


/**
 * 每周食谱
 * Created by Administrator on 2016/9/1 0001.
 */
public class EveryRecipeActivity extends BaseActivity {
	private static final String TAG = "EveryRecipeActivity";
	private FoodExpandableListAdapter foodExpAdapter;
	private ExpandableListView noticeListview;
	public List<ProEveryRecipe.DataEntity> list = new ArrayList<>();
	private Button wrap_shangzhou, wrap_benzhou, wrap_xiazhou;
	private TextView tv_back, tv_title, fill_add;
	private EveryRecipeActivity activity;

	public void updateData() {
		if (foodExpAdapter == null) {
			foodExpAdapter = new FoodExpandableListAdapter(this,list, EveryRecipeActivity.this);
			noticeListview.setAdapter(foodExpAdapter);

		} else {
			foodExpAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.every_recipe_activity);
		activity = EveryRecipeActivity.this;
	}

	@Override
	public void initView() {
		noticeListview = (ExpandableListView) findViewById(R.id.listview);
		wrap_shangzhou = (Button) findViewById(R.id.wrap_shangzhou);//上周
		wrap_benzhou = (Button) findViewById(R.id.wrap_benzhou);//本周
		wrap_xiazhou = (Button) findViewById(R.id.wrap_xiazhou);//下周
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.every_recipe));
		fill_add.setText(getString(R.string.add));
		BaseProtocol.getInstance().changeWeek("thisweek", activity);
	}

	@Override
	public void initListener() {
		wrap_shangzhou.setOnClickListener(this);
		wrap_benzhou.setOnClickListener(this);
		wrap_xiazhou.setOnClickListener(this);
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);

		noticeListview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				for (int i = 0; i < list.size(); i++) {
					if (groupPosition != i) {
						noticeListview.collapseGroup(i);
					}
				}
			}
		});
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				Intent intent = new Intent();
				intent.setClass(this, MenuToAddActivity.class);
				startActivityForResult(intent,Constant.REQUEST_CODE);
				break;
			case R.id.wrap_shangzhou:
				wrap_shangzhou.setTextColor(getResources().getColor(R.color.app_color));
				wrap_benzhou.setTextColor(getResources().getColor(R.color.black));
				wrap_xiazhou.setTextColor(getResources().getColor(R.color.black));
				BaseProtocol.getInstance().changeWeek("onweek", activity);
				break;
			case R.id.wrap_benzhou:
				wrap_shangzhou.setTextColor(getResources().getColor(R.color.black));
				wrap_benzhou.setTextColor(getResources().getColor(R.color.app_color));
				wrap_xiazhou.setTextColor(getResources().getColor(R.color.black));
				BaseProtocol.getInstance().changeWeek("thisweek", activity);
				break;
			case R.id.wrap_xiazhou:
				wrap_shangzhou.setTextColor(getResources().getColor(R.color.black));
				wrap_benzhou.setTextColor(getResources().getColor(R.color.black));
				wrap_xiazhou.setTextColor(getResources().getColor(R.color.app_color));
				BaseProtocol.getInstance().changeWeek("nextweek", activity);
				break;

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.i(TAG, "onActivityResult: requestCode = " + requestCode);
		Log.i(TAG, "onActivityResult: resultCode = " + resultCode);
		if (requestCode == Constant.REQUEST_CODE_ADD && resultCode == Constant.REFRESH_ADAPTER) {
			BaseProtocol.getInstance().changeWeek("thisweek", activity);
		}
		if (requestCode ==Constant.REQUEST_CODE &&resultCode == Constant.REFRESH_ADAPTER){
			BaseProtocol.getInstance().changeWeek("thisweek", activity);
		}
	}
}